<template>
  <div class="login-page">
    
    <div class="layer flex-center">
      <div class="layer bg" id="login"></div>
      <!-- logo部分 -->
      <div class="logo-group">
        <!-- <img src="@/assets/image/logo/w500.png" alt="logo"> -->
      </div>
      <!-- 表单部分 -->
      <div class="form-group">
        <el-card>
          <el-form ref="loginForm" label-position="top" :rules="rules" :model="formLogin">
            <el-form-item  prop="username">
              <el-input type="text" v-model="formLogin.username" placeholder="用户名">
                <template slot="prepend">
                    账号
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input type="password" v-model="formLogin.password" placeholder="密码">
                <template slot="prepend">
                    密码
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="code">
              <el-input type="text" v-model="formLogin.code" placeholder="请输入验证码" >
                <template slot="prepend">验证码</template>
                <template slot="append">
                    <span @click="yanZnFn" >{{yanzNum}}</span>
                </template>
              </el-input>
            </el-form-item>
            <el-button @click="login" type="primary" class="button-login">登陆</el-button>
          </el-form>
        </el-card>
      </div>
      <!-- 帮助按钮 -->
      <!-- <el-button type="info" class="button-help">
        需要帮助
        <i class="fa fa-question-circle"></i>
      </el-button> -->
    </div>
  </div>
</template>

<script>
/* eslint-disable */
require('particles.js')
// 配置地址
// https://vincentgarreau.com/particles.js/#default
import config from './default'
import Cookies from 'js-cookie'
import { loginApi  } from '@/api/table'
export default {
  data () {
    return {
      formLogin: {
        username: '',
        password: '',
        code: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      yanzNum:""
    }
  },
  mounted () {
    // 初始化例子插件
    particlesJS('login', config)
    this.yanZnFn()
    
  },
  methods: {
    // 提交登陆信息
    login () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
           //this.$router.push({ path: '/' })
          if(this.yanzNum!=this.formLogin.code){
              this.$message({
                showClose: true,
                message: '验证码不正确',
                type: 'warning'
              });
              return false
          }
           
            let msg = {
              accName:this.formLogin.username,
              accPwd:this.formLogin.password
            }
            window.localStorage.removeItem("islogin")
            loginApi.userLogin(msg)
            .then(data=>{
               console.log(data)
               let token = data.sessionId
              this.$store.dispatch('setTokens', token )
              window.localStorage.setItem("token",data.sessionId)
               this.$router.push({ path: '/' })
            })
            .catch(error => {
                console.error(error)
            })
        } else {
          return false
        }
      })
    },
    yanZnFn(){
      this.yanzNum=parseInt(Math.random()*(10000-999+1)+999)
    },
  }
}
</script>

<style lang="scss">
@import './style.scss';
</style>

